
cc.Class({
    extends: cc.Component,

    properties: {
        Fy_FriendsGuessDialects:cc.Node,
        GuessDialect:cc.Node,
        content:cc.Node,
        CommentBlock:cc.Node,
        Gd_TopicMaker:cc.Sprite,
        Gd_MakerName:cc.Label,
        Gd_Tips:cc.Label,
        Gd_Expression:cc.Sprite,
        Gd_Subject:cc.Label,
        Gd_Commentary:cc.Label,
        Gd_Barrage:cc.Node,
        Gd_Answer:cc.EditBox,
        Cb_Comment:cc.Prefab,
        Barrage:cc.Prefab,
        Gd_AnswerTips:cc.Node
    },
    onReturn(){
        this.node.active = false;
        clearTimeout(this.myVar); 
    },
    info(ClickId){
        this.worksId = ClickId;
        this.Gd_Answer.string = "";
        this.Gd_Barrage.removeAllChildren(true);
        this.CommentBlock.removeAllChildren(true);
        this.node.active = true;
        this.onCreativeInformation(ClickId);
        this.onComment();
    },
    onCreativeInformation(ClickId){ // 更换作品信息
        var self = this;
        const db = wx.cloud.database();
        db.collection('gatherWorks').doc(ClickId).get({
            success(res) {
                self.dialect = res.data.dialect; //答案
                self.Gd_MakerName.string = res.data.openName+" 创作";
                self.Gd_Tips.string = "提示:"+"两个字的生活用品";
                self.Gd_Subject.string = res.data.dialect;
                cc.loader.load({url: res.data.portrait, type: 'png'}, function (err, texture) {
                    self.Gd_TopicMaker.spriteFrame = new cc.SpriteFrame(texture);
                });
            }
        });
        // this.Gd_Expression.spriteFrame = ""
    },
    onMake(){
        this.node.active = false;
        clearTimeout(this.myVar); 
        this.Fy_FriendsGuessDialects.active = true;
    },
    onChange(){  // 改变内容
        // this.onCreativeInformation();
        console.log("更换内容")
    },
    onScreenshotsSharing(){  // 截图分享
        ddz.onScreenshotsSharing();
    },
    onComment(){ // 评论区
        var data = {
            url:UserData.UserInfo.avatarUrl,
            name:UserData.UserInfo.nickName,
            Answer:"猜对了",
            Time:"一天前",
            LikeQuantity:"999"
        };
        this.Gd_Commentary.string = "666人回答，50人回答正确";
        var shuliang = 10;
        for(var i = 0; i<shuliang;i++){
            var Barrages = cc.instantiate(this.Cb_Comment);
            Barrages.getComponent("Comment").init(data);
            Barrages.y = -(75+i*150);
            Barrages.x = 0;
            this.CommentBlock.addChild(Barrages);
        }
        this.CommentBlock.height = shuliang>2? i*150:230;
        this.content.height = this.CommentBlock.height+1050;
        var self = this;
        var step = function(){
            self.onBarrage(data.url,data.Answer);
            shuliang = shuliang-1;
           if(shuliang>0){
            self.myVar = setTimeout(step, 1000);
           }else{
             clearTimeout(self.myVar) 
           } 
        }
        self.myVar = setTimeout(step, 1000);
    },
    onResponder(){   //回答按钮点击
        var string = this.Gd_Answer.string;
        if(string.length > 1){
            var self = this;
            wx.cloud.callFunction({
              name: 'postComment',
              data: {
                worksId: self.worksId ,           // 作品id
                commentatorId: UserData.UserInfo.openId,     // 评论者的openId
                commentatorName: UserData.UserInfo.nickName,   // 评论者的名字
                commentatorportrait: UserData.UserInfo.avatarUrl,//评论者的头像Url
                commentContent: string,    // 评论内容
                commentaryTime: new Date().getTime(),    // 评论时间
                commentarylike: 0,    // 点赞数    
              },
              complete(res){
                  if(res.result == 1){
                        console.log("发布成功");
                        if(self.dialect == string){
                            self.onBarrage(UserData.UserInfo.avatarUrl,"猜对了");
                        }else self.onBarrage(UserData.UserInfo.avatarUrl,string);
                        ddz.onLoadingData.onMyAnswer();
                  }else console.log("发布失败");
              }
           });
        }else{
            this.Gd_AnswerTips.active = true;
            this.scheduleOnce(function() {
                this.Gd_AnswerTips.active = false;
            }, 1);
        }
    },
    onBarrage(url,string){ // 发弹幕
        var Barrages = cc.instantiate(this.Barrage);
        Barrages.getComponent("Barrage").init(url,string);
        Barrages.y = parseInt(Math.random()*401-140,10);
        Barrages.x = 440;
        this.Gd_Barrage.addChild(Barrages);
        var seq = cc.sequence(
                  cc.moveBy(3, cc.v2(-(440*2), 0)),
                  cc.callFunc(function () {
                    Barrages.destroy()
                  }, this)
            );
        Barrages.runAction(seq)
    }
});
